package com.yunze.quartz.task.yunze.bill;

import com.alibaba.fastjson.JSON;
import com.yunze.common.core.domain.entity.SysUser;
import com.yunze.common.core.domain.model.LoginUser;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.spring.SpringUtils;
import com.yunze.common.utils.yunze.VeDate;
import org.apache.http.client.HttpClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;



/**
 * 定时任务 账单定时任务
 */
@Component("BillTask")
public class BillTask {


    @Resource
    private RabbitTemplate rabbitTemplate;

    public void BillAdd() {
        //1.创建路由 绑定 生产队列 发送消息
        //创建 队列 每月进行账单生成
        String polling_queueName = "admin_Bill_queue";
        String polling_routingKey = "admin.Bill.queue";
        String polling_exchangeName = "admin_exchange";//路由
        try {
            //rabbitMQConfig.creatExchangeQueue(polling_exchangeName, polling_queueName, polling_routingKey, null, null, null, BuiltinExchangeType.DIRECT);
            Map<String, Object> month = new HashMap<>();
//            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
//            SysUser currentUser = loginUser.getUser();
//            String dept_id = currentUser.getDeptId().toString();

            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            System.out.println(authentication);

            //获取 当前月份 如 2020-01-01 00:00:00
            month.put("fist", VeDate.getNowMonthFistDayCZC());//当前月份的第一天
            month.put("last", VeDate.getNowMonthLastDayCZC());//当前月份的最后一天
            rabbitTemplate.convertAndSend(polling_exchangeName, polling_routingKey, JSON.toJSONString(month), message -> {
                // 设置消息过期时间 30 分钟 过期
                message.getMessageProperties().setExpiration("" + (30 * 1000 * 60));
                return message;
            });
        } catch (Exception e) {
            System.out.println("账单同步 失败 " + e.getMessage().toString());
        }
    }

    public static void main(String[] args) {
        String nowMonthFistDayCZC = VeDate.getNowMonthFistDayCZC();
        String nowMonthLastDayCZC = VeDate.getNowMonthLastDayCZC();
        System.out.println("这个月第一天:"+nowMonthFistDayCZC);
        System.out.println("这个月最后一天:"+nowMonthLastDayCZC);

    }

}
